/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spring;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author spring
 */
public class DVBut extends javax.swing.JButton {

    int cw, ch;
    double mx, my;
    boolean showAttMat = false;
    String link=null;
    double[] docvec;
    double[] attractionMatrix;

    public DVBut() {
        super();
        //java.awt.Insets in = new java.awt.Insets(0,0,0,0);
        //javax.swing.border.EmptyBorder eb = new javax.swing.border.EmptyBorder(in);
        //this.setBorder(eb);
        this.setBounds(0, 0, 20, 30);
        this.setBackground(new Color(200, 255, 255));

        final JPopupMenu menu = new JPopupMenu();
        JMenuItem item;
        // Create and add a menu item
        item = new JMenuItem("Weights");
        item.addActionListener(actionListener);
        menu.add(item);
        item = new JMenuItem("Page");
        item.addActionListener(actionListener);
        menu.add(item);
        item = new JMenuItem("Other");
        item.addActionListener(actionListener);
        menu.add(item);

        // Set the component to show the popup menu
        this.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent evt) {
                if (evt.isPopupTrigger()) {
                    menu.show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }

            public void mouseReleased(MouseEvent evt) {
                if (evt.isPopupTrigger()) {
                    menu.show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        });

        this.addActionListener(new ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent e) {
                String command = e.getActionCommand();

                if (command.equals("Weights")) {
                    showHideWeights();
                } else if (command.equals("Page")) {
                    showURL();
                } else if (command.equals("Other")) {
                    // perform Other operation
                }
            }
        });

    }

    public double[] getdoubleVector() {
        return docvec;
    }

    public void setdoubleVector(double[] in) {
        docvec = in;
    }
    
    public void setLink(String in) {
        link = in;
    }
    public String getLink() {
       return link;
    }
    public void setNameVector(String nandv) {
    }

    public void parentresize(int w, int h) {
        cw = w;
        ch = h;
        this.setLocation((int) ((mx * cw / 2) + (cw / 2) - 30), (int) ((my * ch / 2) + (ch / 2) - 30));
    }

    public void setcoords(double x, double y) {
        mx = x;
        my = y;
        cw = this.getParent().getWidth();
        ch = this.getParent().getHeight();
        this.setLocation((int) ((mx * (cw / 2)) + cw / 2), (int) ((my * (ch / 2) + ch / 2)));
    }

    public double[] getcoords() {
        double[] mp = new double[2];
        mp[0] = mx;
        mp[1] = my;
        return mp;
    }

    public void setAttractMatrix(double[] am) {
        attractionMatrix = am;
    }

    public double[] getAttractMatrix() {
        return attractionMatrix;
    }

    public boolean showAttraction() {
        return showAttMat;
    }

    public void showHideWeights() {
        //System.out.println("x = "+mx+", y = "+my);
        if (showAttMat == false) {
            showAttMat = true;
        //((PlotPanelPOI)this.getParent()).setActiveDoc(this);
        // System.out.println("AttractMatrix = "+printdvec(attractionMatrix));
        } else {
            showAttMat = false;
        //((PlotPanelPOI)this.getParent()).setActiveDoc(null);
        }
        ((PlotPanelPOI) this.getParent()).repaint();
    }

    public void showURL(){
        DisplayURL du = new DisplayURL(new JFrame(), false);
        du.setURL(link);
        du.setVisible(true);
    }

    private String printdvec(double[] a) {
        String matrix = new String("{");
        for (int i = 0; i < a.length; i++) {
            matrix += a[i] + ",";
        }
        return matrix.substring(0, matrix.length() - 1) + "}";
    }

    @Override
    protected void processMouseMotionEvent(MouseEvent e) {
        if (e.CONTAINER_EVENT_MASK == java.awt.event.MouseEvent.CONTAINER_EVENT_MASK &&
                (e.getID() == java.awt.event.MouseEvent.MOUSE_PRESSED ||
                e.getID() == java.awt.event.MouseEvent.MOUSE_DRAGGED)) {
            Point p = this.getLocation();
            cw = this.getParent().getWidth();
            ch = this.getParent().getHeight();

        } else {
            super.processMouseMotionEvent(e);
        }
    }
}
